home *** CD-ROM | disk | FTP | other *** search
- /*
- */
-
- parse arg fModifyMe sReplaceMe sWithMe
-
- address COMMAND
-
- sReplaceMe = ChangeToSpaces(sReplaceMe)
- sWithMe = strip(ChangeToSpaces(sWithMe))
-
- fBackupFile = fModifyMe || ".bak"
- say "File to be modified = [" || fModifyMe || "]"
- say "Backup file = [" || fBackupFile || "]"
- say "String to add/replace = [" || sReplaceMe || "]"
- say "add/Replace with = [" || sWithMe || "]"
-
- keylength = length(sReplaceMe)
-
- /* First, make sure rexxsupport.library is available. */
- call addlib("rexxsupport.library", 0, -30, 0)
-
- /* Delete any .bak file that now exists */
- call delete(fBackupFile)
-
- /* Rename the original filename to filename.bak */
- if (rename(fModifyMe, fBackupFile) == 0) then do
- say "Error: Couldn't rename " || fModifyMe || " to " || fBackupFile
- exit(30)
- end
- BFileRenamed = 1
-
- /* Default == haven't found the line we want to replace yet */
- BFoundOurLine = 0
-
- /* Open the backup file for reading */
- if (open('oldfile',fBackupFile,'R') == 0) then do
- say "Couldn't open backup file " || fBackupFile
- call error
- end
-
- /* and the new file for writing */
- if (open('newfile',fModifyMe,'W') == 0) then do
- say "Couldn't open output file " || fModifyMe
- call error
- end
-
-
- /* Scan the old file, writing the new one */
- do while (EOF('oldfile') == 0)
- nextline = readln('oldfile')
- sCheckPart = left(nextline,keylength)
-
- if ((BFoundOurLine == 0)&(sCheckPart = sReplaceMe)) then
- do
- /* don't write out this line... write out our substitute instead! */
- say "Found: [" || nextline || "] replacing with [" || sWithMe || "]"
- call writeln('newfile',sWithMe)
- BFoundOurLine = 1
- end
- else do
- call writeln('newfile',nextline)
- end
- end
-
- /* If we never found our line to replace, then just add our
- line at the end of the file. */
- if (BFoundOurLine == 0) then call writeln('newfile',sWithMe)
-
- call close('oldfile')
- call close('newfile')
-
- /* success! */
- successMessage = "File " || fModifyMe || " successfully modified (" || sReplaceMe || ") -> " || "(" || sWithMe || ")."
-
- say successMessage
-
- /* Show calling app that we succeeded */
- address COMMAND 'echo ' || successMessage || ' >t:edit_text_succeeded'
- exit(0)
-
-
- /* Changes all occurrences of the character '^' to spaces in the string.
- Needed to get around ARexx's lame argument parsing. I think.
- */
- ChangeToSpaces: procedure
- parse arg sOrig
-
- sNew = ""
- do while (length(sOrig) > 0)
- cChar = left(sOrig,1)
- if (cChar == "^") then cChar = " "
- sNew = sNew || cChar
- sOrig = right(sOrig,length(sOrig)-1)
- end
- return sNew
-
-
- /* Replaces original file if an error occurs */
- error:
- say "An error occured!"
- call close('oldfile')
- call close('newfile')
- if (BFileRenamed == 1) then do
- say "Attempting to restore the original file " || fModifyMe
-
- /* Delete the incomplete file that now exists */
- call rename(fModifyMe, fModifyMe || ".tobedeleted")
-
- /* Rename filename.bak back to filename */
- if (rename(fBackupFile, fModifyMe) == 0) then do
- call rename(fModifyMe || ".tobedeleted", fModifyMe)
- say "Error: Couldn't rename " || fBackupFile || " to " || fModifyMe || ", aborting now."
- exit(30)
- end
-
- call delete(fModifyMe||".tobedeleted")
- say "Recovery of original file " || fModifyMe || " was successful."
- end
- exit(30)